//
//  UIScrollView+MMRefresh.m
//  MMSDK
//
//  Created by 君若见故 on 16/4/27.
//  Copyright © 2016年 isoftstone. All rights reserved.
//  pod 'MJRefresh', '~> 3.1.0'

#import "UIScrollView+MMRefresh.h"
#import <MJRefresh/MJRefresh.h>

@implementation UIScrollView (MMRefresh)

#pragma mark - header Refresh
/**
 *  创建普通的header,block
 *
 *  @param block 刷新回调
 */
- (void)addNormalHeaderRefreshWithBlock:(refreshBlock)block {

    MJRefreshNormalHeader *header = (MJRefreshNormalHeader*)[MJRefreshNormalHeader headerWithRefreshingBlock:block];
    header.lastUpdatedTimeLabel.textColor = [UIColor whiteColor];
    header.stateLabel.textColor = [UIColor whiteColor];
    self.mj_header = header;
}
/**
 *  创建普通的header,target
 *
 *  @param obj      目标
 *  @param selector 方法
 */
- (void)addNormalHeaderRefreshWithTarget:(id)obj action:(SEL)selector {

    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:obj refreshingAction:selector];
}
/**
 *  创建动画header,block
 *
 *  @param block 刷新回调
 */
- (void)addGifHeaderRefreshWithBlock:(refreshBlock)block {

    self.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:block];
}
/**
 *  创建动画header,target
 *
 *  @param obj      目标
 *  @param selector 方法
 */
- (void)addGifHeaderRefreshWithTarget:(id)obj action:(SEL)selector {
    
    self.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:obj refreshingAction:selector];
}
/**
 *  是否显示标题标签
 *
 *  @param flag bool
 */
- (void)isShowStateLabel:(BOOL)flag {
    
    MJRefreshStateHeader *header = (MJRefreshStateHeader *)self.mj_header;
    header.stateLabel.hidden = !flag;
}
/**
    *  是否显示时间标签
    *
    *  @param flag bool
    */
- (void)isShowTimeLabel:(BOOL)flag {

    MJRefreshStateHeader *header = (MJRefreshStateHeader *)self.mj_header;
    header.lastUpdatedTimeLabel.hidden = !flag;
}

#pragma mark - Footer Refresh
/**
 *  创建普通的footer,block
 *
 *  @param block 刷新回调
 */
- (void)addAutoNormalFooterRefreshWithBlock:(refreshBlock)block {

    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:block];
}
/**
 *  创建普通的footer,target
 *
 *  @param obj      目标
 *  @param selector 方法
 */
- (void)addAutoNormalFooterRefreshWithTarget:(id)obj action:(SEL)selector {

    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:obj refreshingAction:selector];
}
/**
 *  创建动画footer,block
 *
 *  @param block 刷新回调
 */
- (void)addAutoGifFooterRefreshWithBlock:(refreshBlock)block {

    self.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:block];
}
/**
 *  创建动画footer,target
 *
 *  @param obj      目标
 *  @param selector 方法
 */
- (void)addAutoGifFooterRefreshWithTarget:(id)obj action:(SEL)selector {

    self.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:obj refreshingAction:selector];
}
/**
 *  创建自动回弹的footer,block
 *
 *  @param block 刷新回调
 */
- (void)addBackNormalFooterRefreshWithBlock:(refreshBlock)block {
    
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:block];
}
/**
 *  创建自动回弹的block,target
 *
 *  @param obj      目标
 *  @param selector 方法
 */
- (void)addBackNormalFooterRefreshWithTarget:(id)obj action:(SEL)selector {

    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:obj refreshingAction:selector];
}
/**
 *  创建自动回弹动画的block,block
 *
 *  @param block 刷新回调
 */
- (void)addBackGifFooterRefreshWithBlock:(refreshBlock)block {
    
    self.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:block];
}
/**
 *  创建自动回弹动画的block,target
 *
 *  @param obj      目标
 *  @param selector 方法
 */
- (void)addBackGifFooterRefreshWithTarget:(id)obj action:(SEL)selector {
    
    self.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:obj refreshingAction:selector];
}

//删除上拉加载
- (void)removeFooterRefresh{
    if (self.mj_footer) {
        [self.mj_footer setHidden:YES];
    }
}

/**
 *  是否显示footer标题
 *  @part MMRefreshPartAutoFooter/MMRefreshPartBackFooter
 *  @param flag bool
 */
- (void)isPart:(MMRefreshPart)part ShowTitleLabel:(BOOL)flag {

    if (part == MMRefreshPartAutoFooter) {
        MJRefreshAutoStateFooter *autoStateFooter = (MJRefreshAutoStateFooter *)self.mj_footer;
        autoStateFooter.refreshingTitleHidden = !flag;
    } else {
        MJRefreshBackStateFooter *backFooter = (MJRefreshBackStateFooter *)self.mj_footer;
        backFooter.stateLabel.hidden = !flag;
    }
}
/**
 *  没有更多数据
 */
- (void)setNoMoreData {

    [self.mj_footer endRefreshingWithNoMoreData];
}
/**
 *  重置没有更多数据状态
 */
- (void)resetNoMoreData {
    
    [self.mj_footer resetNoMoreData];
}

#pragma mark - Common
/**
 *  创建标题
 *
 *  @part  MMRefreshPartHeader/MMRefreshPartAutoFooter/MMRefreshPartBackFooter
 *  @param str   标题
 *  @param state 状态
 */
- (void)setPart:(MMRefreshPart)part headerTitle:(NSString *)str forState:(MMRefreshState)state {

    if (part == MMRefreshPartHeader) {
        MJRefreshStateHeader *header = (MJRefreshStateHeader *)self.mj_header;
        [header setTitle:str forState:[self transforState:state]];
    } else if (part == MMRefreshPartAutoFooter) {
        MJRefreshAutoStateFooter *autoStateFooter = (MJRefreshAutoStateFooter *)self.mj_footer;
        [autoStateFooter setTitle:str forState:[self transforState:state]];
    } else {
        MJRefreshBackStateFooter *backStateFooter = (MJRefreshBackStateFooter *)self.mj_footer;
        [backStateFooter setTitle:str forState:[self transforState:state]];
    }
}
/**
 *  创建动画，在动画header/footer时有效
 *
 *  @part  MMRefreshPartHeader/MMRefreshPartAutoFooter/MMRefreshPartBackFooter
 *  @param list  动画数组
 *  @param state 状态
 */
- (void)setPart:(MMRefreshPart)part images:(NSArray *)list forState:(MMRefreshState)state {

    if (part == MMRefreshPartHeader) {
        
        if ([self.mj_header isKindOfClass:[MJRefreshGifHeader class]]) {
            MJRefreshGifHeader *gifHeader = (MJRefreshGifHeader *)self.mj_header;
            [gifHeader setImages:list forState:[self transforState:state]];
        } else {
            return;
        }
    } else if(part == MMRefreshPartAutoFooter) {
        if ([self.mj_footer isKindOfClass:[MJRefreshAutoGifFooter class]]) {
            MJRefreshAutoGifFooter *autoGifFooter = (MJRefreshAutoGifFooter *)self.mj_footer;
            [autoGifFooter setImages:list forState:[self transforState:state]];
        } else {
            return;
        }
    } else {
        if ([self.mj_footer isKindOfClass:[MJRefreshBackGifFooter class]]) {
            MJRefreshBackGifFooter *backGifFooter = (MJRefreshBackGifFooter *)self.mj_footer;
            [backGifFooter setImages:list forState:[self transforState:state]];
        } else {
            return;
        }
    }
}

/**
 *  开始刷新
 *  @part  MMRefreshPartHeader/MMRefreshPartAutoFooter/MMRefreshPartBackFooter
 */
- (void)beginRefresh:(MMRefreshPart)part {
    
    if (part == MMRefreshPartHeader) {
        [self.mj_header beginRefreshing];
    } else {
        [self.mj_footer beginRefreshing];
    }
}
/**
 *  是否正在刷新
 *
 *  @part  MMRefreshPartHeader/MMRefreshPartAutoFooter/MMRefreshPartBackFooter
 *  @return bool
 */
- (BOOL)isRefreshing:(MMRefreshPart)part {
    
    if (part == MMRefreshPartHeader) {
        return [self.mj_header isRefreshing];
    } else {
        return [self.mj_footer isRefreshing];
    }
}
/**
 *  结束刷新
 *  @part  MMRefreshPartHeader/MMRefreshPartAutoFooter/MMRefreshPartBackFooter
 */
- (void)endRefresh:(MMRefreshPart)part {
    
    if (part == MMRefreshPartHeader) {
        [self.mj_header endRefreshing];
    } else {
        [self.mj_footer endRefreshing];
    }
}
/**
 *  状态码转换
 *
 *  @param state MMRefreshState
 *
 *  @return MJRefreshState
 */
- (MJRefreshState)transforState:(MMRefreshState)state {
    
    if (state == MMRefreshStateIdle) {
        return MJRefreshStateIdle;
    } else if (state == MMRefreshStatePulling) {
        return MJRefreshStatePulling;
    } else if (state == MMRefreshStateRefreshing) {
        return MJRefreshStateRefreshing;
    } else {
        return 0;
    }
}

@end



















